package me.miencraft.plugin.superlobby.listener;

import me.miencraft.plugin.superlobby.SuperLobby;
import me.miencraft.plugin.superlobby.item.JoinItem;
import me.miencraft.plugin.superlobby.util.EntityDamageCause;
import me.miencraft.plugin.superlobby.util.VisiblePlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.*;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;

/**
 * @author langle__
 * @version 1.0
 */
public class PlayerEvent implements Listener {

    @EventHandler(priority = EventPriority.MONITOR)
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();

        PlayerInventory inventory = player.getInventory();
        inventory.setHeldItemSlot(SuperLobby.mainConfig.getDefaultSlot());
        inventory.clear();
        for (JoinItem joinItem : SuperLobby.mainConfig.getJoinItemList()) {
            joinItem.giveItemToPlayer(player);
        }
        inventory.setItem(SuperLobby.mainConfig.getHideItemSlot(), SuperLobby.mainConfig.getVisibleItem().toItemStack());
        VisiblePlayer.hidePlayerFromVisible(player);
        SuperLobby.spawnConfig.teleportPlayer(player);
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onPlayerQuit(PlayerQuitEvent event) {
        Player player = event.getPlayer();

        VisiblePlayer.disableVisibility(player);
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onPlayerRespawn(PlayerRespawnEvent event) {
        Player player = event.getPlayer();

        SuperLobby.spawnConfig.teleportPlayer(player);
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onEntityDamage(EntityDamageEvent event) {
        Entity entity = event.getEntity();
        EntityDamageEvent.DamageCause cause = event.getCause();

        if (!(entity instanceof Player)) return;

        if (cause == EntityDamageEvent.DamageCause.SUICIDE || cause == EntityDamageCause.Kill) return;
        if (cause == EntityDamageEvent.DamageCause.VOID) {
            SuperLobby.spawnConfig.teleportPlayer(entity);
        }

        event.setDamage(0.0);
        event.setCancelled(true);
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        EquipmentSlot hand = event.getHand();
        Action action = event.getAction();
        ItemStack item = event.getItem();

        if (hand == EquipmentSlot.OFF_HAND || action == Action.PHYSICAL || item == null) return;

        if (SuperLobby.mainConfig.getVisibleItem().validate(item)) {
            SuperLobby.mainConfig.getVisibleItem().onItemUse(player);
        } else if (SuperLobby.mainConfig.getHideItem().validate(item)) {
            SuperLobby.mainConfig.getHideItem().onItemUse(player);
        } else {
            for (JoinItem joinItem : SuperLobby.mainConfig.getJoinItemList()) {
                if (joinItem.validate(item)) {
                    joinItem.onItemUse(player);
                    break;
                }
            }
        }
    }

    @EventHandler
    public void onPlayerDropItem(PlayerDropItemEvent event) {
        Player player = event.getPlayer();
        Item item = event.getItemDrop();
        ItemStack stack = item.getItemStack();

        if (SuperLobby.mainConfig.getVisibleItem().validate(stack)) {
            event.setCancelled(true);
        } else if (SuperLobby.mainConfig.getHideItem().validate(stack)) {
            event.setCancelled(true);
        } else {
            for (JoinItem joinItem : SuperLobby.mainConfig.getJoinItemList()) {
                if (joinItem.validate(stack)) {
                    event.setCancelled(true);
                    break;
                }
            }
        }
    }

}
